home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / GMSMTH01.ZIP / INCLUDE / POLYGON.H < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-28  |  12.8 KB  |  325 lines

  1. #ifndef INC_POLYGON
  2. #define INC_POYGON 1
  3.  
  4. #ifndef _XLIB_H_
  5. extern unsigned short ScrnLogicalByteWidth;   /* Virtual screen width in bytes       */
  6. extern unsigned short ScrnLogicalPixelWidth;  /* Virtual screen width in pixels      */
  7. extern unsigned short ScrnLogicalHeight;      /* Virtual screen height in pixels     */
  8. extern short page;
  9. extern unsigned short HiddenPageOffs;
  10. extern unsigned short VisiblePageOffs;
  11. #endif
  12.  
  13.    
  14. /* POLYGON.H: Header file for filled polygon drawing and 3D animation. */
  15.  
  16. #define MAX_OBJECTS  100   /* max simultaneous # objects supported */
  17. #define MAX_POLY_LENGTH 4  /* four vertices is the max per poly */
  18.  
  19. /* Ball movement flags */
  20. #define MOVE_LEFT       0x0001
  21. #define MOVE_RIGHT      0x0002
  22. #define MOVE_UP         0x0004
  23. #define MOVE_DOWN       0x0008
  24. #define MOVE_TOWARD     0x0010
  25. #define MOVE_AWAY       0x0020
  26. #define FLIP_SPIN_AXIS  0x0040
  27.  
  28.  
  29. /* Maximum number of simultaneously active spotlights */
  30. #define MAX_SPOTS 3
  31.  
  32. /* Ratio: distance from viewpoint to projection plane / width of
  33.    projection plane. Defines the width of the field of view. Lower
  34.    absolute values = wider fields of view; higher values = narrower */
  35. #define PROJECTION_RATIO -131072l /* -2 negative because visible Z
  36.                                  coordinates are negative */
  37.  
  38. /* Draws the polygon described by the point list PointList in color
  39.    Color with all vertices offset by (X,Y) */
  40. #define DRAW_POLYGON(PointList,NumPoints,Color,X,Y)          \
  41.    Polygon.Length = NumPoints; Polygon.PointPtr = PointList; \
  42.    FillConvexPolygon(&Polygon, Color, X, Y);
  43.  
  44. /* Draws the polygon described by the point list PointList with a bitmap
  45.    texture mapped onto it */
  46. #define DRAW_TEXTURED_POLYGON(PointList,NumPoints,TexVerts,TexMap) \
  47.    Polygon.Length = NumPoints; Polygon.PointPtr = PointList;       \
  48.    DrawTexturedPolygon(&Polygon, TexVerts, TexMap);
  49.  
  50. #define INT_TO_FIXED(x) (((long)(int)x) << 16)
  51. #define FIXED_TO_INT(FixedVal) ((int) (FixedVal >> 16))
  52.  
  53. // #define ROUND_FIXED_TO_INT(FixedVal) ((int) ((FixedVal + DOUBLE_TO_FIXED(0.5)) >> 16))
  54. // #define DOUBLE_TO_FIXED(x) ((long) (x * 65536.0 + 0.5))
  55. // #define FIXED_TO_DOUBLE(x) (((double)x) / 65536.0)
  56.  
  57.  
  58. #define MIN_Z -3276800l          //(INT_TO_FIXED(-50))
  59.  
  60. #if 0
  61. /* Sets a color intensity to the specified levels */
  62. #define SET_INTENSITY(IntensityTemp, R, G, B) \
  63.    IntensityTemp.Red = R;       \
  64.    IntensityTemp.Green = G;     \
  65.    IntensityTemp.Blue = B;
  66.  
  67. /* Converts a color component value between 0 and 1 to an R, G, or B
  68.    value between 0 and 255. Allows expressing color components as fractions
  69.    in the range 0 and 1, although internally (in ModelColor structures)
  70.    they're actually represented as values between 0 and 255 */
  71. //#define CCOMP(Color) ((Color<0) ? 0 : ((Color>=1) ? 255 : ((double)Color*255.0+0.5)))
  72. #endif
  73.  
  74. /* Calculates dot product */
  75. #define DOT_PRODUCT(V1,V2) \
  76.    (FixedMul(V1.X,V2.X)+FixedMul(V1.Y,V2.Y)+FixedMul(V1.Z,V2.Z))
  77.  
  78. /* Retrieves the specified pixel from the specified image bitmap of the
  79.    specified width. */
  80. #define GET_IMAGE_PIXEL(TexMapBits, TexMapWidth, X, Y) \
  81.    TexMapBits[(Y * TexMapWidth) + X]
  82.  
  83. /* Masks to mark shading types in Face structure */
  84. #define NO_SHADING      0x0000
  85. #define AMBIENT_SHADING 0x0001
  86. #define DIFFUSE_SHADING 0x0002
  87. #define TEXTURE_MAPPED_SHADING 0x0004
  88.  
  89. typedef long Fixedpoint;
  90. typedef unsigned short TAngle;  /* angle in tenths of degrees */
  91. typedef Fixedpoint Xform[3][4];
  92.  
  93. /* Describes a single 2D point */
  94. typedef struct _Point {
  95.    short X;
  96.    short Y;
  97. } Point;
  98.  
  99. /* Describes a color in the current color model, the RGB color cube */
  100. typedef struct _ModelColor {
  101.    unsigned char Red;   /* 255 = max red, 0 = no red */
  102.    unsigned char Green; /* 255 = max green, 0 = no green */
  103.    unsigned char Blue;  /* 255 = max blue, 0 = no blue */
  104. } ModelColor;
  105.  
  106. /* Describes an intensity in the current color model, the RGB color cube */
  107. typedef struct _ModelIntensity {
  108.    Fixedpoint Red;
  109.    Fixedpoint Green;
  110.    Fixedpoint Blue;
  111. } ModelIntensity;
  112.  
  113. /* Describes a single 3D point in homogeneous coordinates; the W
  114.    coordinate isn't present, though; assumed to be 1 and implied */
  115. typedef struct _Point3 {
  116.    Fixedpoint X;
  117.    Fixedpoint Y;
  118.    Fixedpoint Z;
  119. } Point3;
  120.  
  121. typedef struct {
  122.    short X;
  123.    short Y;
  124.    short Z;
  125. } IntPoint3;
  126.  
  127. /* Describes a series of points (used to store a list of vertices that
  128.    describe a polygon; each vertex is assumed to connect to the two
  129.    adjacent vertices; last vertex is assumed to connect to first) */
  130. typedef struct {
  131.    short Length;
  132.    Point * PointPtr;
  133. } PointListHeader;
  134.  
  135. /* Describes the beginning and ending X coordinates of a single
  136.    horizontal line */
  137. typedef struct {
  138.    short XStart;
  139.    short XEnd;
  140. } HLine;
  141.  
  142. /* Describes a Length-long series of horizontal lines, all assumed to
  143.    be on contiguous scan lines starting at YStart and proceeding
  144.    downward (used to describe a scan-converted polygon to the
  145.    low-level hardware-dependent drawing code) */
  146. typedef struct {
  147.    short Length;
  148.    short YStart;
  149.    HLine * HLinePtr;
  150. } HLineList;
  151.  
  152. /* Describes a rectangle */
  153. typedef struct {
  154.    short Left;
  155.    short Top;
  156.    short Right;
  157.    short Bottom;
  158. } Rect;
  159.  
  160. /* Describes a texture map */
  161. typedef struct {
  162.    short TexMapWidth;  /* texture map width in bytes */
  163.    char far *TexMapBits; /* pointer to texture bitmap */
  164. } TextureMap;
  165.  
  166. /* Structure describing one face of an object (one polygon) */
  167. typedef struct {
  168.    short * VertNums;   /* pointer to list of indexes of this polygon's
  169.                         vertices in the object's vertex list. The first two
  170.                         indexes must select the end and start points,
  171.                         respectively, of this polygon's unit normal vector.
  172.                         The second point should also be an active polygon
  173.                         vertex */
  174.    short NumVerts;     /* # of verts in face, not including the initial
  175.                         vertex, which must be the end of a unit normal vector
  176.                         that starts at the second index in VertNums */
  177.    short ColorIndex;   /* direct palette index; used only for non-shaded
  178.                         faces */
  179.    short ShadingType;  /* none, ambient, diffuse, texture mapped, etc. */
  180.    TextureMap * TexMap; /* pointer to bitmap for texture mapping, if any */
  181.    Point * TexVerts; /* pointer to list of this polygon's vertices, in
  182.                         TextureMap coordinates. Index n must map to index
  183.                         n + 1 in VertNums, (the + 1 is to skip over the unit
  184.                         normal endpoint in VertNums) */
  185. } Face;
  186.  
  187. typedef struct { TAngle RotateX, RotateY, RotateZ; } RotateControl;
  188.  
  189. /* Fields common to every object */
  190. #define BASE_OBJECT                                              \
  191.    struct _Object *NextObject;                                   \
  192.    struct _Object *PreviousObject;                               \
  193.    Point3 CenterInView;    /* coord of center in view space */   \
  194.    short RecalcXform;        /* 1 to indicate need to recalc */    \
  195. /* Basic object */
  196. typedef struct _Object { BASE_OBJECT } Object;
  197. /* Structure describing a polygon-based object */
  198. typedef struct {
  199.    BASE_OBJECT
  200.    short RDelayCount, RDelayCountBase; /* controls rotation speed */
  201.    short MDelayCount, MDelayCountBase; /* controls movement speed */
  202.    Xform XformToWorld;        /* transform from object->world space */
  203.    Xform XformToView;         /* transform from object->view space */
  204.    RotateControl Rotate;      /* controls rotation change over time */
  205.    short NumVerts;              /* # vertices in VertexList */
  206.    short NumRealVerts;          /* # vertices that aren't unit normal
  207.                                  endpoints. Unit normal endpoints must appear
  208.                                  last in VertexList, and aren't transformed
  209.                                  into screen space or screen coordinates */
  210.    Point3 * VertexList;       /* untransformed vertices */
  211.    Point3 * XformedVertexList;   /* transformed into view space */
  212.    Point3 * ProjectedVertexList; /* projected into screen space */
  213.    Point * ScreenVertexList;     /* converted to screen coordinates */
  214.    short NumFaces;              /* # of faces in object */
  215.    Face * FaceList;           /* pointer to face info */
  216.    short not_draw;
  217. } PObject;
  218.  
  219.  
  220. /* Describes the current location and stepping, in both the source and
  221.    the destination, of an edge. Mirrors structure in DRAWTEXP.C. */
  222. typedef struct {
  223.    int Direction;    /* through edge list; 1 for a right edge (forward
  224.                         through vertex list), -1 for a left edge (backward
  225.                         through vertex list) */
  226.    int RemainingScans;  /* height left to scan out in dest */
  227.    int CurrentEnd;      /* vertex # of end of current edge */
  228.    Fixedpoint SourceX;     /* current X location in source for this edge */
  229.    Fixedpoint SourceY;     /* current Y location in source for this edge */
  230.    Fixedpoint SourceStepX; /* X step in source for X step in dest of 1 */
  231.    Fixedpoint SourceStepY; /* Y step in source for X step in dest of 1 */
  232.                         /* variables used for all-integer Bresenham's-type
  233.                            Y stepping through the dest, needed for precise
  234.                            pixel placement to avoid gaps */
  235.    int DestY;           /* current Y location in dest for this edge */
  236.    int DestYIntStep;    /* whole part of dest Y step per column X step */
  237.    int DestYDirection;  /* -1 or 1 to indicate which way Y steps
  238.                            (left/right) */
  239.    int DestYErrTerm;    /* current error term for dest Y stepping */
  240.    int DestYAdjUp;      /* amount to add to error term per column move */
  241.    int DestYAdjDown;    /* amount to subtract from error term when the
  242.                            error term turns over */
  243. } EdgeScan;
  244.  
  245. extern Fixedpoint lsqrt(Fixedpoint r);
  246. extern void XformVec(Xform, Fixedpoint *, Fixedpoint *);
  247. extern void ConcatXforms(Xform, Xform, Xform);
  248. extern short FillConvexPolygon(PointListHeader *, short, short, short);
  249. extern void SetGraphicsMode(void);
  250. extern void ShowPage(unsigned short);
  251. extern void FillRectangleX(short, short, short, short, unsigned short, short);
  252. extern void XformAndProjectPObject(PObject *);
  253. extern void DrawPObject(PObject *);
  254. extern void AppendRotationX(Xform, TAngle);
  255. extern void AppendRotationY(Xform, TAngle);
  256. extern void AppendRotationZ(Xform, TAngle);
  257. extern Fixedpoint FixedMul(Fixedpoint, Fixedpoint);
  258. extern Fixedpoint FixedDiv(Fixedpoint, Fixedpoint);
  259. extern void InitializeFixedPoint(void);
  260. extern void RotateAndMoveBall(PObject *);
  261. extern void InitializeCubes(void);
  262. extern void InitializeBalls(unsigned char far *);
  263. extern void CosSin(TAngle, Fixedpoint *, Fixedpoint *);
  264. extern void AddObject(Object *);
  265. extern void SortObjects(void);
  266. extern void InitializeObjectList(void);
  267.  
  268. #if 0
  269. extern short ModelColorToColorIndex(ModelColor * Color);
  270. extern void IntensityAdjustColor(ModelColor *, ModelColor *,
  271.    ModelIntensity *);
  272. extern void InitializeLighting(void);
  273. extern void SetAmbientIntensity(ModelIntensity *);
  274. extern ModelIntensity * GetAmbientIntensity(void);
  275. extern void TurnSpotOn(short);
  276. extern void TurnSpotOff(short);
  277. extern void SetSpotDirection(short, Point3 *);
  278. extern void SetSpotIntensity(short, ModelIntensity *);
  279. extern Point3 * GetSpotDirection(short);
  280. extern ModelIntensity * GetSpotIntensity(short);
  281. extern short GetSpotState(short);
  282. extern void TurnAmbientOn(void);
  283. extern void TurnAmbientOff(void);
  284. extern short GetAmbientState(void);
  285. #endif
  286.  
  287. extern void DrawTexturedPolygon(PointListHeader *, Point *, TextureMap *);
  288. extern void WritePixelX(short, short, short);
  289. extern short RecalcAllXforms;
  290. extern short NumObjects;
  291. extern Object ObjectListStart, ObjectListEnd;
  292. extern Xform WorldViewXform;
  293. extern Object *ObjectList[];
  294. extern short PolyClipMinX, PolyClipMinY, PolyClipMaxX, PolyClipMaxY;
  295.  
  296. // extern short DisplayedPage, NonDisplayedPage,
  297. // extern Point3 CubeVerts[];
  298. //extern unsigned short CurrentPageBase;
  299. //extern unsigned short PageStartOffsets[2];
  300.  
  301. #if 0
  302. extern ModelIntensity AmbientIntensity;
  303. extern Point3 SpotDirectionWorld[];
  304. extern Point3 SpotDirectionView[];
  305. extern ModelIntensity SpotIntensity[];
  306. extern short SpotOn[];
  307. extern short AmbientOn;
  308. #endif
  309.  
  310. extern short BallEvent;
  311.  
  312. extern void DrawHorizontalLineList(HLineList *, short, unsigned char);
  313. extern void ScanOutLine(EdgeScan *, EdgeScan *, unsigned short Poffs);
  314. extern void ScanEdge(short, short, short, short, short, short, HLine **);
  315. extern Fixedpoint FixedMul(Fixedpoint M1, Fixedpoint M2);
  316. extern Fixedpoint FixedDiv(Fixedpoint Dividend, Fixedpoint Divisor);
  317.  
  318. extern Point3 WorldCentre;
  319. extern short ViewAngle;
  320. extern short PolyCentreX, PolyCentreY;
  321. extern Fixedpoint PolyScreenWidth;
  322.  
  323. #endif
  324. 
  325.